package com.yonyou.pmclouds.basecom.conf;

import com.yonyou.pmclouds.basecom.interceptor.PrintParamInterceptor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author xiejianyu
 * 2019-09-03
 */
@Configuration
public class PrintParamInterceptorConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry){

        PrintParamInterceptor interceptor = new PrintParamInterceptor();
        /*
        //添加排除的路径
        List<String> ignoredPaths = new ArrayList<>();
        ignoredPaths.add("/xxxxxx");
        interceptor.setIgnoredPaths(ignoredPaths);
        if(CollectionUtils.isNotEmpty(ignoredPaths)){
            List<Pattern> ignoredPatterns = new ArrayList<Pattern>();
            Iterator<String> _it = ignoredPaths.iterator();
            while(_it.hasNext()){
                String ignore = _it.next();
                Pattern pattern = Pattern.compile(ignore);
                ignoredPatterns.add(pattern);
            }
            interceptor.setIgnoredPatterns(ignoredPatterns);
        }
        */
        InterceptorRegistration interceptorRegistry = registry.addInterceptor(interceptor);
        interceptorRegistry.addPathPatterns("/**");
    }
}
